From 9cec7b8e1d7738898c027850f70fe70d0862eaca Mon Sep 17 00:00:00 2001 From: justbur Date: Mon, 6 Jul 2015 13:23:19 -0400 Subject: [PATCH] Refactor replacement alists --- which-key.el | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/which-key.el b/which-key.el index 7c7e16753ba..7571f7242c3 100644 --- a/which-key.el +++ b/which-key.el @@ -29,13 +29,14 @@ "Truncate the description of keys to this length. Also adds \"..\".") (defvar which-key-key-replacement-alist - '((">". "") ("<" . "") ("left" ."←") ("right" . "→")) - "The strings in the car of each cons cell are replaced with the + '(("<\\(\\(C-\\|M-\\)*.+\\)>" . "\\1") ("\\(left\\)" ."←") + ("\\(right\\)" . "→")) + "The strings in the car of each cons cell are replaced with the strings in the cdr for each key.") -(defvar which-key-general-replacement-alist +(defvar which-key-description-replacement-alist '(("Prefix Command" . "prefix")) "See `which-key-key-replacement-alist'. This is a list of cons -cells for replacing any text, keys and descriptions.") +cells for replacing descriptions.") (defvar which-key-buffer-name "*which-key*" "Name of which-key buffer.") (defvar which-key-popup-type 'minibuffer @@ -303,14 +304,12 @@ of the intended popup." key-match desc-match unformatted formatted) (with-temp-buffer (describe-buffer-bindings buffer key) - (which-key/replace-strings-from-alist which-key-general-replacement-alist) (goto-char (point-max)) ; want to put last keys in first (while (re-search-backward (format "^%s \\([^ \t]+\\)[ \t]+\\(\\(?:[^ \t\n]+ ?\\)+\\)$" key-str-qt) nil t) - (setq key-match (s-replace-all - which-key-key-replacement-alist (match-string 1)) + (setq key-match (match-string 1) desc-match (match-string 2) max-len-key (max max-len-key (length key-match)) max-len-desc (max max-len-desc (length desc-match))) @@ -364,15 +363,24 @@ of the intended popup." (goto-char (point-min))))) (cons act-height act-width))) -(defun which-key/replace-strings-from-alist (replacements) +(defun which-key/perform-replacements (key-desc-cons-list key-reps desc-reps &optional literal) "Find and replace text in buffer according to REPLACEMENTS, which is an alist where the car of each element is the text to replace and the cdr is the replacement text." - (dolist (rep replacements) - (save-excursion - (goto-char (point-min)) - (while (or (search-forward (car rep) nil t)) - (replace-match (cdr rep) t t))))) + (mapcar + (lambda (el) + (let ((key (car el)) (desc (cdr el))) + (dolist (key-rep key-reps) + (setq key + (if (string-match (car key-rep) key) + (replace-match (cdr key-rep) t literal key) + key))) + (dolist (desc-rep desc-reps) + (setq desc + (if (string-match (car desc-rep) desc) + (replace-match (cdr desc-rep) t literal desc) + desc))) + (cons key desc))) key-desc-cons-list)) (defsubst which-key/truncate-description (desc) "Truncate DESC description to `which-key-max-description-length'." @@ -385,6 +393,9 @@ replace and the cdr is the replacement text." strings (including text properties), and pad with spaces so that all are a uniform length. MAX-LEN-KEY and MAX-LEN-DESC are the longest key and description in the buffer, respectively." + (setq unformatted (which-key/perform-replacements + unformatted which-key-key-replacement-alist + which-key-description-replacement-alist nil)) (mapcar (lambda (key-desc-cons) (let* ((key (car key-desc-cons)) -- 2.30.2